package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.OrgDto;
import com.hualude.roadsign.dto.OrgFatherAndChildDto;
import com.hualude.roadsign.repository.OrgFatherAndChildRepository;
import com.hualude.roadsign.repository.OrgRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.OrgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu-jiaHui on 2017/2/25.
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Resource
    private OrgRepository orgRepository;

    @Resource
    private OrgFatherAndChildRepository orgFatherAndChildRepository;

    public DTResponseDto getOrg4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception {
        List<OrgDto> dto4DTs = orgRepository.getOrg4DT(dtRequestDto, actorId, orgId);
        Integer count = orgRepository.getCount4DT(dtRequestDto, actorId, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }

    @Transactional
    public Integer addOrg(OrgDto orgDto) throws Exception {
        return orgRepository.save(orgDto.toBean()).getOrgId();
    }

    @Transactional
    public Integer updateOrgById(OrgDto orgDto) throws Exception {
        if (orgDto.getOrgType() == 1) {
            orgFatherAndChildRepository.deleteMainOrgByOrgId(orgDto.getOrgId());
        }
        Integer data = orgRepository.updateOrgById(orgDto.getOrgName(), orgDto.getOrgPerson(), orgDto.getOrgPersonMobile(),
                orgDto.getOrgMobile(), orgDto.getOrgFax(), orgDto.getOrgWeb(), orgDto.getOrgRemark(),
                orgDto.getOrgId(), orgDto.getOrgType(), orgDto.getOrgAddress(), orgDto.getOrgEmail());
        if (data > 0)
            return orgDto.getOrgId();
        else return 0;
    }

    @Transactional
    public Integer deactivateOrgByIds(String orgIds, Integer orgState) throws Exception {
        String[] orgIdArray = orgIds.split(",");
        int i;
        if (orgState == 0) {
            for (i = 0; i < orgIdArray.length; i++) {
                int orgId = Integer.parseInt(orgIdArray[i]);
                orgRepository.deactivateOrgById(orgId);
            }
        } else {
            for (i = 0; i < orgIdArray.length; i++) {
                int orgId = Integer.parseInt(orgIdArray[i]);
                orgRepository.activateOrgById(orgId);
            }
        }
        return i;
    }

    public List<OrgDto> getMainOrgNameAndId(Integer orgId) throws Exception {
        List<OrgDto> orgDtos = orgRepository.getMainOrgId(orgId);
        for (OrgDto orgDto : orgDtos) {
            orgDto.setOrgName(orgRepository.getMainOrgName(orgDto.getOrgId()));
        }
        return orgDtos;
    }

    public List<OrgDto> getOrgNameAndId() throws Exception {
        return orgRepository.getOrgNameAndId();
    }


    public String getOrgFatherNameByOrgId(Integer orgId) throws Exception {
        String result = "";
        List<String> orgFathers = orgRepository.getOrgFatherNameByOrgId(orgId);
        if (orgFathers == null || orgFathers.size() == 0)
            return result;
        result += orgFathers.get(0);
        int len = orgFathers.size();
        for (int i = 1; i < len; i++) {
            result += "/";
            result += orgFathers.get(i);
        }
        return result;
    }

    public DTResponseDto getCandidateMainOrgIdAndNameByOrgId(DTRequestDto dtRequestDto, Integer orgId) throws Exception {
        if (orgId == null) {
            List<OrgDto> orgDtos = orgRepository.getAllMainOrg(dtRequestDto.getSearch());
            Integer count = orgRepository.getAllMainOrgCount(dtRequestDto.getSearch());
            DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, orgDtos);
            return dtResponseDto;
        }
        List<OrgDto> orgDtos = orgRepository.getCandidateMainOrgIdAndNameByOrgId(dtRequestDto, orgId);
        Integer count = orgRepository.getCandidateMainOrgCountByOrgId(dtRequestDto, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, orgDtos);
        return dtResponseDto;
    }

    public DTResponseDto getMainOrgIdAndNameByOrgId(DTRequestDto dtRequestDto, Integer orgId) throws Exception {
        List<OrgDto> orgDtos = orgFatherAndChildRepository.getMainOrgIdAndNameByOrgId(dtRequestDto, orgId);
        Integer count = orgFatherAndChildRepository.getMainOrgCountByOrgId(dtRequestDto, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, orgDtos);
        return dtResponseDto;
    }

    @Transactional
    public Integer addMainOrg4OrgId(Integer orgId, Integer mainOrgId) throws Exception {
        OrgFatherAndChildDto orgFatherAndChildDto = new OrgFatherAndChildDto(orgId, mainOrgId);
        return orgFatherAndChildRepository.save(orgFatherAndChildDto.toBean()).getOrgFatherAndChildId();
    }

    @Transactional
    public Integer deleteMainOrgByIds(Integer orgId, Integer mainOrgId) throws Exception {
        return orgFatherAndChildRepository.deleteOrgFatherAndChildByOrgId(orgId, mainOrgId);
    }

    public List<String> getOrgNameByOrgId(Integer orgId, Integer orgType) throws Exception {
        if (orgType == 1)
            return orgFatherAndChildRepository.getOrgIdNameByOrgId(orgId);
        else
            return orgFatherAndChildRepository.getMainOrgNameByOrgId(orgId);
    }

    public Integer getCountByOrgName(String orgName) throws Exception {
        return orgRepository.getCountByOrgName(orgName);
    }

    @Transactional
    public Integer deleteOrgByOrgId(Integer orgId) throws Exception {
        orgFatherAndChildRepository.deleteAllByOrgId(orgId);
        return orgRepository.deleteOrgByOrgId(orgId);
    }

    @Override
    @Transactional
    public Integer addMenu(OrgDto orgDto) throws Exception {
        return orgRepository.addMenu(orgDto.getOrgId(), orgDto.getOrgMenu());
    }

    @Override
    public String getOrgMenu(Integer orgId) throws Exception {
        return orgRepository.getOrgMenu(orgId);
    }
}
